package top.psjj.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.psjj.ssm.entity.User;
import top.psjj.ssm.service.UserService;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login(User user){
        System.out.println("1:"+user);
        User temp = userService.login(user);
        System.out.println(temp.getId());
        if (temp == null){
            return "";
        }
        //转发到查询所有数据
        return "redirect:/user/list";
    }

    @RequestMapping("/list")
    public String list(User user,Model model){
        List<User> userList = userService.list(user);
        model.addAttribute("userList",userList);
        return "user/list";
    }

    @RequestMapping("detail")
    public String detail(Integer id,Model model){
        System.out.println(id);
        User temp = new User();
        if (id != null){
            temp = userService.selectById(id);
        }
        model.addAttribute("user",temp);
        return "user/update";
    }

    @RequestMapping("save")
    public String save(User user){
        userService.save(user);
        return "redirect:/user/list";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        userService.deleteById(id);
        return "redirect:/user/list";
    }

    @GetMapping("/login")
    public String hello(Model model){
        return "login";
    }
}
